$(function () {
  // $(".taskList-pop").addClass("is-visible3");
  $(".index .combinedActionTaskBack").on("click", function (event) {
    $(".taskList-pop").addClass("is-visible3");
  });
  var getPeripheralEquipmentArr = [];
  // 协同作战 任务单 显示
  $(".combinedActionTask-target").on("click", function (event) {
    console.log(smokeCombinedActionInfoArr);
    $(".taskList-pop").addClass("is-visible3");
    // 获取报警时间
    var taskStepsBeginTimeText = smokeCombinedActionInfoArr[5];
    taskStepsBeginTimeText = moment(taskStepsBeginTimeText).format("YYYY-MM-DD HH:mm:ss");
    $("#taskListModal .taskStepsBeginTimeText").text(taskStepsBeginTimeText);
    var userName = localStorage.getItem('userName');
    $("#taskListModal .taskStepsCurrentTabInitiator").text(userName);
    // 获取 操作时间
    // var currentTime = new Date().toLocaleTimeString();
    var currentTime = new Date();
    currentTime = moment(currentTime).format("YYYY-MM-DD HH:mm:ss");
    $("#taskListModal .taskStepsCurrentTabOperationTime").text(currentTime);
    // 获取报警人与联系方式
    var taskListDataAlarmContact1 = smokeCombinedActionInfoArr[9];
    var taskListDataAlarmContactNum1 = smokeCombinedActionInfoArr[12];
    var taskListDataAlarmContact2 = smokeCombinedActionInfoArr[10];
    var taskListDataAlarmContactNum2 = smokeCombinedActionInfoArr[13];
    var taskListDataAlarmContact3 = smokeCombinedActionInfoArr[11];
    var taskListDataAlarmContactNum3 = smokeCombinedActionInfoArr[14];
    $("#taskListModal .taskListDataAlarmContact1").text(taskListDataAlarmContact1);
    $("#taskListModal .taskListDataAlarmContactNum1").text(taskListDataAlarmContactNum1);
    $("#taskListModal .taskListDataAlarmContact2").text(taskListDataAlarmContact2);
    $("#taskListModal .taskListDataAlarmContactNum2").text(taskListDataAlarmContactNum2);
    $("#taskListModal .taskListDataAlarmContact3").text(taskListDataAlarmContact3);
    $("#taskListModal .taskListDataAlarmContactNum3").text(taskListDataAlarmContactNum3);
    // 获取报警地点
    var address = smokeCombinedActionInfoArr[1];
    $("#taskListModal .taskListDataAlarmAddress").val(address);
    // 获取消火栓分布和 周边协同点 数据
    var devCode = smokeCombinedActionInfoArr[0];
    $('#taskListModal .fireHydrantDistribution .fireHydrantDistributionTabBigBox').html('');
    $('#taskListModal .cooPointAround .cooPointAroundTabBigBox').html('');
    $.ajax({
      // http://localhost:9092/workOrder/getPeripheralEquipment?devCode=c4cd8232e6e2ee21
      url: '/workOrder/getPeripheralEquipment?devCode='+smokeCombinedActionInfoArr[0],
      type: 'GET',
      success: function (result) {
        console.log(result);
        var content = result.data;
        getPeripheralEquipmentArr = content;
        var mUnm = 0;
        for (var i = 0; i < content.length; i++) {
          // 获取距离
          var distance = content[i].distance;
          // 获取消火栓分布数据
          if (content[i].device) {
            var fireHydrantDistributionName = content[i].device.deviceType.deviceTypeName;
            // console.log(fireHydrantDistributionName);
            var fireHydrantDistributionAddress = content[i].device.address;
            // console.log(fireHydrantDistributionAddress);                    
            $('#taskListModal .fireHydrantDistribution .fireHydrantDistributionTabBigBox').append('<div class="clearfix"><span class="fl" style="width: 33.33%">' + fireHydrantDistributionName + '</span><span class="fl" style="width: 33.33%">' + fireHydrantDistributionAddress + '</span><span class="fl" style="width: 33.33%; color: #3bfffd;">' + distance + '米</span></div>');

          }
          // 获取协同点数据
          if (content[i].manual) {
            mUnm++;
            var cooPointAroundName = content[i].manual.devTypeName;
            console.log(cooPointAroundName)
            if(cooPointAroundName==0){
              cooPointAroundName='社企协同点';
            }else if(cooPointAroundName==1){
              cooPointAroundName='社群协同点';
            }else if(cooPointAroundName==2){
              cooPointAroundName='社警协同点';
            }
            var cooPointAroundAddress = content[i].manual.organization.orgName;
            var manual = content[i].manual;
            manual.id = '';
            // manual.devCode = 1;    
            console.log(JSON.stringify(manual),manual)
            $('#taskListModal .cooPointAround .cooPointAroundTabBigBox').append('<div class="cooPointAroundTabBigList"><label><i></i><input manual="' + content[i].manual.devCode + '" name="manualIdHistory' + mUnm + '" type="checkbox" style="display: none;" value=' + JSON.stringify(manual) + '></label><span style="width: 55.33%" class="cooPointAroundTabBoxManualId1">' + cooPointAroundName + '/' + manual.address + '</span><span style="width: 24.33%; color: #3bfffd;" class="cooPointAroundTabBoxDistance1">' + distance + '米</span></div>');
            for (var j = 0; j < $('#taskListModal .cooPointAround .cooPointAroundTabBigList').length; j++) {
              for (var k = 0; k < combinedActionMarkerArr.length; k++) {
                if ($('#taskListModal .cooPointAround .cooPointAroundTabBigList').eq(j).find('input').attr('manual') == combinedActionMarkerArr[k]) {
                  $('#taskListModal .cooPointAround .cooPointAroundTabBigList').eq(j).find('input').prop('checked', true);
                  $('#taskListModal .cooPointAround .cooPointAroundTabBigList').eq(j).find('i').addClass('active');
                }
              }
            }
          }
        }
      },
      error: function (res) {
        console.log(res);
      }
    });
  });
  // 滚动条
  $(".fireHydrantDistributionTabSmallBoxItem_scroll").yi_scroll();
  $(".cooPointAroundTabSmallBoxItem_scroll").yi_scroll();
  // 协同作战 任务单 关闭
  $(".taskList-pop").on("click", function (event) {
    if (
      $(event.target).is(".cd-popup-close") ||
      $(event.target).is(".taskList-pop")||$(event.target).is(".cd-popup-close1")
    ) {
      event.preventDefault();
      $(this).removeClass("is-visible3");
      // 清除数据
      $("#taskListForm .cooContentTabBox input[type=checkbox]").prop("checked", false);
      $("#taskListForm .cooPointAround .cooPointAroundTabBox input[type=checkbox]").prop("checked", false);
      $("#taskListForm").find('i').removeClass('active');
      $("#taskListForm .taskListRemark").find("textarea").val('');
      $('.combinedActionTaskStyle').show();
      
      SendTaskListFormValidator();    
      
    }
  });

  // 周边协同点 点击打勾
  // var cooPointAroundNum1 = 0;
  // var synergeticContentNum1 = 0;
  $(".taskListModal .cooPointAroundTabBigBox").on("change", "input[type='checkbox']", function () {
    $('#taskListForm .cooPointAroundTabBigBox input[type=checkbox]:checked').each(function (i, item) {
      $(this).siblings('i').addClass('active');
      // cooPointAroundNum1++;
      // for (var i = 0; i < getPeripheralEquipmentArr.length; i++) {
      //   if(getPeripheralEquipmentArr[i].manual){

      //     console.log(getPeripheralEquipmentArr[i].manual.devCode)
      //     if (getPeripheralEquipmentArr[i].manual.devCode == $(this).attr('manual')) {
      //       for(var j=0;j<combinedActionMarker.length;j++){
      //         if(combinedActionMarker[j].devCode==$(this).attr('manual')){
      //           combinedActionMarker[j].removePoint();
      //           combinedActionMarker.splice(j, 1);
      //         }
      //       }
      //       // 0为社企协同点
      //       if (getPeripheralEquipmentArr[i].manual.devTypeName == 0) {
      //         combinedActionMarker.push(BaiDuMap.combinedActionAddMarker00('socialCooperation', 'left', getPeripheralEquipmentArr[i].distance, getPeripheralEquipmentArr[i].manual.organization.personal, getPeripheralEquipmentArr[i].manual.address, 'combinedActionNum', 'combinedActionPhone', 'manager', 'telephone', getPeripheralEquipmentArr[i].manual.latitude, getPeripheralEquipmentArr[i].manual.longitude, getPeripheralEquipmentArr[i].manual.devCode, 'position', 'orgName', '消火栓'));
  
      //       } else if (getPeripheralEquipmentArr[i].manual.devTypeName == 1) {
      //         // 1为社群协同点
      //         combinedActionMarker.push(BaiDuMap.combinedActionAddMarker00('communityCooperation', 'left', getPeripheralEquipmentArr[i].distance, getPeripheralEquipmentArr[i].manual.organization.personal, getPeripheralEquipmentArr[i].manual.address, 'combinedActionNum', 'combinedActionPhone', 'manager', 'telephone', getPeripheralEquipmentArr[i].manual.latitude, getPeripheralEquipmentArr[i].manual.longitude, getPeripheralEquipmentArr[i].manual.devCode, 'position', 'orgName', '消火栓'));
  
      //       } else if (getPeripheralEquipmentArr[i].manual.devTypeName == 2) {
      //         //  2位社警协同点
      //         combinedActionMarker.push(BaiDuMap.combinedActionAddMarker00('policeCooperation', 'left', getPeripheralEquipmentArr[i].distance, getPeripheralEquipmentArr[i].manual.organization.personal, getPeripheralEquipmentArr[i].manual.address, 'combinedActionNum', 'combinedActionPhone', 'manager', 'telephone', getPeripheralEquipmentArr[i].manual.latitude, getPeripheralEquipmentArr[i].manual.longitude, getPeripheralEquipmentArr[i].manual.devCode, 'position', 'orgName', '消火栓'));
  
      //       }
      //     }
      //   }

      // }

    });
    $('#taskListForm .cooPointAroundTabBigBox input[type=checkbox]').not("input:checked").each(function (i, item) {
      $(this).siblings('i').removeClass('active');     
      console.log($(this).attr('manual'));
      // for (var i = 0; i < getPeripheralEquipmentArr.length; i++) {
      //   if(getPeripheralEquipmentArr[i].manual){

      //     console.log(getPeripheralEquipmentArr[i].manual.devCode)
      //     if (getPeripheralEquipmentArr[i].manual.devCode == $(this).attr('manual')) {
      //       for(var j=0;j<combinedActionMarker.length;j++){
      //         if(combinedActionMarker[j].devCode==$(this).attr('manual')){
      //           combinedActionMarker[j].removePoint();
      //           combinedActionMarker.splice(j, 1);
      //         }
      //       }
      //       console.log(combinedActionMarker);
      //       // 0为社企协同点
      //       if (getPeripheralEquipmentArr[i].manual.devTypeName == 0) {
      //         combinedActionMarker.push(BaiDuMap.combinedActionAddMarker0('socialCooperation', 'left', getPeripheralEquipmentArr[i].distance, getPeripheralEquipmentArr[i].manual.organization.personal, getPeripheralEquipmentArr[i].manual.address, 'combinedActionNum', 'combinedActionPhone', 'manager', 'telephone', getPeripheralEquipmentArr[i].manual.latitude, getPeripheralEquipmentArr[i].manual.longitude, getPeripheralEquipmentArr[i].manual.devCode, 'position', 'orgName', '消火栓'));
  
      //       } else if (getPeripheralEquipmentArr[i].manual.devTypeName == 1) {
      //         // 1为社群协同点
      //         combinedActionMarker.push(BaiDuMap.combinedActionAddMarker0('communityCooperation', 'left', getPeripheralEquipmentArr[i].distance, getPeripheralEquipmentArr[i].manual.organization.personal, getPeripheralEquipmentArr[i].manual.address, 'combinedActionNum', 'combinedActionPhone', 'manager', 'telephone', getPeripheralEquipmentArr[i].manual.latitude, getPeripheralEquipmentArr[i].manual.longitude, getPeripheralEquipmentArr[i].manual.devCode, 'position', 'orgName', '消火栓'));
  
      //       } else if (getPeripheralEquipmentArr[i].manual.devTypeName == 2) {
      //         //  2位社警协同点
      //         combinedActionMarker.push(BaiDuMap.combinedActionAddMarker0('policeCooperation', 'left', getPeripheralEquipmentArr[i].distance, getPeripheralEquipmentArr[i].manual.organization.personal, getPeripheralEquipmentArr[i].manual.address, 'combinedActionNum', 'combinedActionPhone', 'manager', 'telephone', getPeripheralEquipmentArr[i].manual.latitude, getPeripheralEquipmentArr[i].manual.longitude, getPeripheralEquipmentArr[i].manual.devCode, 'position', 'orgName', '消火栓'));
  
      //       }
      //     }
      //   }

      // }
    });
  })
  // 协同内容 点击打勾
  $(".taskListModal .cooContentTabBox input[type='checkbox']").change(function () {
    $('#taskListForm .cooContentTabBox input[type=checkbox]:checked').each(function (i, item) {
      console.log(this);
      $(this).siblings('i').addClass('active');
      // synergeticContentNum1++;
    });
    $('#taskListForm .cooContentTabBox input[type=checkbox]').not("input:checked").each(function (i, item) {
      console.log(this);
      $(this).siblings('i').removeClass('active');
    });
  })
  // 任务单 发送 上传
  // console.log(cooPointAroundNum1);
  // console.log(synergeticContentNum1);
  // $("#taskListModal .taskListSendBtn-pop").on("click", function(){
  //   if(cooPointAroundNum1 == 0 || synergeticContentNum1 == 0) {
  //     tipModalFunc('协同内容不完整，发送失败!');
  //   } else {
  //     SendTaskListFormValidator();
  //   }
  // })
  SendTaskListFormValidator();
  function SendTaskListFormValidator() {
    $("#taskListForm").bootstrapValidator({
      message: 'This value is not valid',
      feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
      },
      fields: {}
    }).on("success.form.bv", function (e) {
      e.preventDefault();
      var obj = {};
      var orgId = localStorage.getItem('userOrgId');
      var initiator = localStorage.getItem('userName');
      obj.initiator = initiator;
      // 获取表单数据
      var alarmTime = $("#taskListForm").find(".taskStepsBeginTimeText").text();

      alarmTime = moment(alarmTime).valueOf();
      // console.log(alarmTime);
      obj.alarmTime = alarmTime;
      var operationTime = $("#taskListForm").find(".taskStepsCurrentTabOperationTime").text();
      operationTime = moment(operationTime).valueOf();
      obj.operationTime = operationTime;
      // 周边协同点内容
      // var cooPointAroundNum = 0;
      var manualArr = [];
      $("#taskListForm .cooPointAroundTabBigBox input[type=checkbox]:checked").each(function (index, ele) {
        // cooPointAroundNum++;
        var $this = $(this);
        // 以对象形式上传数据
        // var item = $this.attr('name')
        // obj[item] = JSON.parse($this.val());
        console.log($this.val());
        var obj1 = JSON.parse($this.val());
        obj1.accept = null;
        manualArr.push(obj1);
      });
      console.log(manualArr);
      obj.manualHistory = manualArr;
      var synergeticContentText = '';
      var synergeticContent = '';
      // 协同内容
      // var synergeticContentNum = 0;
      $('#taskListForm .cooContentTabBox input[type=checkbox]:checked').each(function (i, item) {
        // synergeticContentNum++;
        synergeticContentText = $(this).parent().siblings("span").text();
        synergeticContent = synergeticContent + synergeticContentText + ' ';
      });
      obj.synergeticContent = synergeticContent;
      var alarmContact = $("#taskListForm").find(".taskListDataAlarmContactBox").text();
      obj.alarmContact = alarmContact;
      var alarmAddress = $("#taskListForm").find(".taskListDataAlarmAddress").val();
      obj.alarmAddress = alarmAddress;
      var remarks = $("#taskListForm").find(".taskListRemark-pop").val();
      obj.remarks = remarks;
      // 状态 2 表示处理中
      var taskState = 2;
      obj.taskState = taskState;
      console.log(obj);
      var jsonData = JSON.stringify(obj);
      console.log(jsonData);
      var cooPointAroundNum = $("#taskListForm .cooPointAroundTabBigBox input[type=checkbox]:checked").length;
      var synergeticContentNum = $('#taskListForm .cooContentTabBox input[type=checkbox]:checked').length;
      console.log(cooPointAroundNum,synergeticContentNum);
      if(cooPointAroundNum == 0 || synergeticContentNum == 0) {
        tipModalFunc('协同内容不完整，发送失败!');
        $("#taskListForm").data('bootstrapValidator').destroy();
        $('#taskListForm').data('bootstrapValidator', null);
        SendTaskListFormValidator();
        // $("#taskListForm").bootstrapValidator({
        //   message: 'This value is not valid',
        //   feedbackIcons: {
        //     valid: 'glyphicon glyphicon-ok',
        //     invalid: 'glyphicon glyphicon-remove',
        //     validating: 'glyphicon glyphicon-refresh'
        //   },
        //   fields: {}
        // }).on("success.form.bv", function (e) {
          
        //   e.preventDefault();
        // });
      } else {
        
        $.ajax({
          // localhost:9091/workOrder/save?orgid=1   json传递WorkOrder对象
          url: '/workOrder/save?orgid=' + orgId,
          type: 'POST',
          async: false,
          data: jsonData,
          dataType: 'json',
          contentType: 'application/json',
          success: function (result) {
            console.log(result);
            if (result.status == 200) {
              $('.taskList-pop').removeClass('is-visible3');
              tipModalFunc('发送成功!');
              //Modal验证销毁重构
              $("#taskListForm").data('bootstrapValidator').destroy();
              $('#taskListForm').data('bootstrapValidator', null);
              SendTaskListFormValidator();
              // 清除数据及样式
              $("#taskListForm .cooContentTabBox input[type=checkbox]").prop("checked", false);
              $("#taskListForm .cooPointAround .cooPointAroundTabBigBox input[type=checkbox]").prop("checked", false);
              $("#taskListForm").find('i').removeClass('active');
              $("#taskListForm .taskListRemark").find("textarea").val('');
              $("#taskListForm .taskListData").find(".taskListDataAlarmAddress").val('');
              $("#taskListForm").find(".taskStepsBeginTimeText").text('');
              $("#taskListForm").find(".taskStepsCurrentTabInitiator").text('');
              $("#taskListForm").find(".taskStepsCurrentTabOperationTime").text('');
              // 清除报警联系人
              $("#taskListForm .taskListData").find(".taskListDataAlarmContact1").text('');
              $("#taskListForm .taskListData").find(".taskListDataAlarmContactNum1").text('');
              $("#taskListForm .taskListData").find(".taskListDataAlarmContact2").text('');
              $("#taskListForm .taskListData").find(".taskListDataAlarmContactNum2").text('');
              $("#taskListForm .taskListData").find(".taskListDataAlarmContact3").text('');
              $("#taskListForm .taskListData").find(".taskListDataAlarmContactNum3").text('');
              // mUnm = 0;  //清除协同点的数据记录

              // 任务单发送成功后处理报警
              var disposeStatus = 1;
              var alarmId = smokeAlarmData[alarmHandleNum][6];
              var devCode = smokeAlarmData[alarmHandleNum][0];
              smokeAlarmData.splice(alarmHandleNum, 1)
              if (smokeAlarmData.length > alarmHandleNum) {
                smokeAlarmRender(alarmHandleNum);
              } else if (alarmHandleNum > 0) {
                alarmHandleNum -= 1;
                smokeAlarmRender(alarmHandleNum);
              } else if (smokeAlarmData.length == 0) {
                $('#smokeAlarm').hide();
                stopSound();
              }
              handleIgnoreAjax(alarmId, disposeStatus, devCode);
              alarmpageTurningBtn();
              // smokeAlarmDataAjax();
      newAlarmFn();
            } else {
              console.log(result.message);
              //Modal验证销毁重构
              $("#taskListForm").data('bootstrapValidator').destroy();
              $('#taskListForm').data('bootstrapValidator', null);
              SendTaskListFormValidator();            
              // 清除数据及样式
              $("#taskListForm .cooContentTabBox input[type=checkbox]").prop("checked", false);
              $("#taskListForm .cooPointAround .cooPointAroundTabBigBox input[type=checkbox]").prop("checked", false);
              $("#taskListForm").find('i').removeClass('active');
              $("#taskListForm .taskListRemark").find("textarea").val('');
              $("#taskListForm .taskListData").find(".taskListDataAlarmAddress").val('');
              $("#taskListForm").find(".taskStepsBeginTimeText").text('');
              $("#taskListForm").find(".taskStepsCurrentTabInitiator").text('');
              $("#taskListForm").find(".taskStepsCurrentTabOperationTime").text('');
              // 清除报警联系人
              $("#taskListForm .taskListData").find(".taskListDataAlarmContact1").text('');
              $("#taskListForm .taskListData").find(".taskListDataAlarmContactNum1").text('');
              $("#taskListForm .taskListData").find(".taskListDataAlarmContact2").text('');
              $("#taskListForm .taskListData").find(".taskListDataAlarmContactNum2").text('');
              $("#taskListForm .taskListData").find(".taskListDataAlarmContact3").text('');
              $("#taskListForm .taskListData").find(".taskListDataAlarmContactNum3").text('');
              // mUnm = 0;
            }
          },
          error:function(res){
            console.log(res);
            //Modal验证销毁重构
            $("#taskListForm").data('bootstrapValidator').destroy();
            $('#taskListForm').data('bootstrapValidator', null);
            SendTaskListFormValidator();          
            // 清除数据及样式
            $("#taskListForm .cooContentTabBox input[type=checkbox]").prop("checked", false);
            $("#taskListForm .cooPointAround .cooPointAroundTabBigBox input[type=checkbox]").prop("checked", false);
            $("#taskListForm").find('i').removeClass('active');
            $("#taskListForm .taskListRemark").find("textarea").val('');
            $("#taskListForm .taskListData").find(".taskListDataAlarmAddress").val('');
            $("#taskListForm").find(".taskStepsBeginTimeText").text('');
            $("#taskListForm").find(".taskStepsCurrentTabInitiator").text('');
            $("#taskListForm").find(".taskStepsCurrentTabOperationTime").text('');
            // 清除报警联系人
            $("#taskListForm .taskListData").find(".taskListDataAlarmContact1").text('');
            $("#taskListForm .taskListData").find(".taskListDataAlarmContactNum1").text('');
            $("#taskListForm .taskListData").find(".taskListDataAlarmContact2").text('');
            $("#taskListForm .taskListData").find(".taskListDataAlarmContactNum2").text('');
            $("#taskListForm .taskListData").find(".taskListDataAlarmContact3").text('');
            $("#taskListForm .taskListData").find(".taskListDataAlarmContactNum3").text('');
            // mUnm = 0;
          }
        });
      }      
    });
  }

});
// var obj={"manualId3":{"id":1,"devName":"手控","devCode":1,"reportTime":"2018-10-17T10:44:59.000+0000","ipAddress":null,"url":null,"accept":true,"longitude":"30.244713570554058","latitude":"120.17053276027792","organization":{"id":1,"orgid":"1","orgName":"杭州市","centrex":"120.175883","centrey":"30.248254","parent":null,"level":0,"path":"1","removeStatus":null,"personal":null,"telephone":null,"type":"street","photoUrl":null,"address":null}},"initiator":"馒头山消防站","operationTime":"2018-10-20 14:11:26","synergeticContent":"","alarmContact":"        \n                      陆国庆\n                      社区消防      \n                      社区消防\n                      86054046 \n                      86554119\n                      15397091065                  \n                  ","alarmAddress":"二商场19号","remarks":"                "}